package com.example.color_test.service.Impl;

import com.example.color_test.entity.Record;
import com.example.color_test.mapper.RecordMapper;
import com.example.color_test.service.RecordService;
import com.example.color_test.utils.ResultCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * record
 * @author 大狼狗
 * @date 2021/02/21
 */
@Service
public class RecordServiceImpl implements RecordService {

    @Resource
    private RecordMapper recordMapper;


    @Override
    public Object insert(Record record) {

        // valid
        if (record == null) {
            return "必要参数缺失";
        }

        recordMapper.insert(record);
        return  ResultCode.SUCCSSES.Result("测试成功",record);
    }


    @Override
    public Object delete(int id) {
        int ret = recordMapper.delete(id);
        return ret>0?"成功":"失败";
    }


    @Override
    public Object update(Record record) {
        int ret = recordMapper.update(record);
        return ret>0?"成功":"失败";
    }


    @Override
    public Record load(int id) {
        return recordMapper.load(id);
    }


    @Override
    public Map<String,Object> pageList(int offset, int pagesize) {

        List<Record> pageList = recordMapper.pageList(offset, pagesize);
        int totalCount = recordMapper.pageListCount(offset, pagesize);

        // result
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("pageList", pageList);
        result.put("totalCount", totalCount);

        return result;
    }

}